package cn.jas.common.jascommon.util;

import cn.jas.common.jascommon.common.Constants;
import cn.jas.common.jascommon.exception.JasXmlParseException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: xml解析工具
 * @author: chen_mx
 **/
public class XmlUtils {
    @SuppressWarnings("all")
    public  static  List<Map<String,Object>>  parseDropOrgContent(String xml){
        SAXReader  reader=new SAXReader();
        List<Map<String,Object>> list=new ArrayList<>();
        Document  doc= null;
        Map<String,Object> map=new HashMap<>();
        try {
            doc = reader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
            Element root = doc.getRootElement();
            Element body = root.element("Body");
            List<Element> elements = body.element("organization").elements("WebServiceOrg");//.elements().size();
            parseMap(elements,list);
        }catch (Exception e){
            e.printStackTrace();
        }

        return list;
    }


    public  static  List<Map<String,Object>>  parseDropUserContent(String xml){
        SAXReader  reader=new SAXReader();
        List<Map<String,Object>> list=new ArrayList<>();
        Document  doc= null;
        Map<String,Object> map=new HashMap<>();
        try {
            doc = reader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
            Element  root=doc.getRootElement();
            Element body=root.element("Body");
            List<Element> elements= body.element("userinfo").elements("WebServiceUser");//.elements().size();
            parseMap(elements,list);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static List<Map<String,Object>> parseUserContent(String   xml){
        List<Map<String,Object>> list=new ArrayList<>();
        SAXReader  reader=new SAXReader();
        Map<String,Object> map=null;
        try {
            Document  doc= reader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
            Element  root=doc.getRootElement();
            Element body=root.element("Body");
            List<Element> elements= body.element("userinfo").elements("WebServiceUser");//.elements().size();
            parseMap(elements,list);
        } catch (Exception e) {
            e.printStackTrace();
            throw   new JasXmlParseException("err@用户xml解析异常!");
        }
        return list;
    }


    public  static  List<Map<String,Object>>  parseOrgContent(String xml){
        List<Map<String,Object>>  list=new ArrayList<>();
        SAXReader  reader=new SAXReader();
        Map<String,Object> map=null;
        try {
            Document  doc=reader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
            Element  root=doc.getRootElement();
            Element body=root.element("Body");
            List<Element> elements= body.element("organization").elements("WebServiceOrg");
            parseMap(elements,list);
        } catch (Exception e) {
            throw   new JasXmlParseException("err@机构xml解析异常!");
        }
        return list;
    }
    @SuppressWarnings("all")
    public  static  void   parseMap(List<Element> elements,List<Map<String,Object>> list){
        Map<String,Object>  map=new HashMap<>();
        for(Element element:elements){
            List<Element> elss= element.elements();
            if(elss.size()>1){
                map=new HashMap<>();
                for(Element elt:elss){
                    map.put(elt.getName().toLowerCase(),elt.getText());
                }
                list.add(map);
            }else {
                map=new HashMap<>();
                map.put(element.getName().toLowerCase(),element.getText());
                list.add(map);
            }
        }
    }



    public static void main(String[] args) {
       /* System.out.println(parseOrgContent(org));
        System.out.println(parseOrgContent(org).size());*/


    }

}
